<script setup lang="ts">
import type { ArticleItem, ArticleResData } from '@/types/data'
import axios from 'axios'
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'

const list = ref<ArticleItem[]>([])
import { useChannelStore } from '@/stores/channel'
const store = useChannelStore()

watch(
  () => store.channelID,
  async () => {
    // console.log('频道变了')
    const res = await axios.get<ArticleResData>('http://geek.itheima.net/v1_0/articles', {
      params: {
        channel_id: store.channelID,
        timestamp: Date.now(),
      },
    })
    // console.log(res.data)
    list.value = res.data.data.results
  },
  {
    immediate: true,
  },
)
const router = useRouter()
const toDetail = (id: string) => {
  router.push({
    path: '/detail',
    query: {
      id: id,
    },
  })
}
</script>

<template>
  <div class="article-list">
    <div
      class="article-item"
      v-for="item in list"
      :key="item.art_id"
      @click="toDetail(item.art_id)"
    >
      <p class="title">{{ item.title }}</p>
      <!-- template 解析完毕之后不会出现,但是可以写指令 -->
      <template v-if="item.cover.type > 0">
        <img v-for="(it, index) in item.cover.images" :key="index" class="img" :src="it" alt="" />
      </template>
      <div class="info">
        <span>{{ item.aut_name }}</span>
        <span>{{ item.comm_count }} 评论</span>
        <span>{{ item.pubdate }}</span>
      </div>
    </div>
  </div>
</template>
